﻿@namespace CyssBlazor.Shared.Components

<div class="modal fade" id="@Id" tabindex="-1" role="dialog" data-backdrop="false" data-keyboard="false" aria-hidden="true" @ref=ModalElement>
    <div class="modal-dialog">
        <div class="modal-content" style="@ModalStyles">
            <div class="modal-header">
                <h5 class="modal-title">@Title</h5>
                <button aria-label="Close" type="button" class="btn btn-close" role="button" @onclick=@OnClose><!--!--></button>
            </div>

            <div class="modal-body">
                @ChildContent
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

@code
{
    [Parameter]
    public RenderFragment ChildContent { get; set; }


    public ElementReference ModalElement { get; set; }

    [Inject]
    public ModalService modalService { set; get; }

    [Parameter]
    public string Id { set; get; }

    [Parameter]
    public string Title { set; get; }

    /// <summary>
    /// 宽度
    /// </summary>
    [Parameter]
    public int Width { set; get; }

    /// <summary>
    /// 关闭的时间是否清除modal
    /// </summary>
    public bool IsClear { set; get; } = true;


    private string ModalStyles
    {
        get
        {

            return Width > 0 ? $"width:{Width}px" : "";
        }
    }

    /// <summary>
    /// 关闭
    /// </summary>
    private void OnClose()
    {
        if (IsClear)
        {
            modalService.Remove(this.Id);
        }
        else
        {
            modalService.Close(this.Id);
        }
    }
}